import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import {nextTick,getCurrentInstance} from 'vue'
import { useDataStore } from '@/store/data';
import {INITSA} from "@/api";
const routes: Array<RouteRecordRaw> = [
    {path: '/', name: 'Index', redirect:'/login',},
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '登录',
            keepAlive: true,
            requireAuth: false
        },
        component: () => import('@/pages/login/login.vue')
    },

    {
        path: '/resetPassword',
        name: 'ResetPassword',
        meta: {
            title: '修改密码',
        },
        component:()=>import('@/pages/resetPassword/resetPassword.vue')
    },
    {
        path: '/error',
        name: 'error',
        meta: {
            title: '错误404',
        },
        component:()=>import('@/pages/error.vue')
    },
    {
        path:'/background',
        component:()=>import('@pages/background/background.vue'),
        children:[
                { path:'/offer',component:()=>import('@pages/offer/offer.vue'),meta:{title: '报价列表'}},
                { path:'/offerDetail',component:()=>import('@pages/offer/offerDetail.vue'),meta:{title:'报价已完成详情'}},
                { path:'/offerComplete',component:()=>import('@pages/offer/offerComplete.vue'),meta:{title:'报价已完成列表'}},
                { path:'/bidding',component:()=>import('@pages/bidding/bidding.vue'),meta:{title:'竞价列表'}},
                { path:'/biddingDetail',component:()=>import('@pages/bidding/biddingDetail.vue'),meta:{title:'竞价已完成详情'}},
                { path:'/biddingComplete',component:()=>import('@pages/bidding/biddingComplete.vue'),meta:{title:'竞价已完成列表'}},
                { path:'/litigationProcessing',component:()=>import('@pages/litigationResponsibility/taskList.vue'),meta:{title:'待处理'}},
                { path:'/litigationComplete',component:()=>import('@pages/litigationResponsibility/taskList.vue'),meta:{title:'已完成'}},
                { path:'/litigationDetail',component:()=>import('@pages/litigationResponsibility/taskDetail.vue'),meta:{title:'诉责详情'}},
                { path:'/litigationPersonalCenter',component:()=>import('@pages/litigationResponsibility/personalCenter.vue'),meta:{title:'个人中心'}},
            ]
    }

]
const needLoginURL = ['/offerDetail','/biddingDetail','/offerComplete','/biddingComplete','/bidding','/information','/resetPassword','/task','/taskComplete','/taskDetail','/myQuery','/assessorQuery','/queryDetail','/personalCenter','/dataCollection','/consultation','/recycleNew','/recycleComplete','/recoveryDetail','/handleDetail','/litigationProcessing','/litigationComplete','/litigationDetail','/litigationPersonalCenter','/litReset','/expert','/expertDetail','/expertPersonalCenter'];

const router = createRouter({
    history: createWebHistory(),
    routes
});



const currentInstance=getCurrentInstance()
const $sensors=currentInstance?.appContext.config.globalProperties

router.beforeEach(function (to,from,next) {

    if(to.path!='/'){
        const flag = needLoginURL.some(function (value) {
            return to.path===value
        })
        if(flag){
            if(sessionStorage.getItem('tokenId')){
                if(sessionStorage.getItem('orgCode')=='1'){
                    window.sessionStorage.setItem('orgCode','2')
                    router.push('/resetPassword').then(r => console.log(r)).catch()
                }else{
                    next()
                }
            }else {
                next('/')
            }
        }else {
            if(to.path!='/error')next()
            else
                router.push('/error').then(r => console.log(r)).catch()
        }
    }else {
        next()
    }
})
router.afterEach(function (to,from) {
    // @ts-ignore
    document.title = to.meta.title
    const store=useDataStore()
    nextTick(() => {
        if (store.sensorsOpen) { // @ts-ignore
            $sensors.quick('autoTrackSinglePage', INITSA)
        }
    }).then(r  =>{})
})
export default router;
